# -*- coding: utf-8 -*-
from datetime import timedelta

from jms.dwd import jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
from jms.dwd.dwd_s01_whole_operations_trace_mid_dt import jms_dwd__dwd_s01_whole_operations_trace_mid_dt
from utils.operators.spark_sql_operator import SparkSqlOperator
from jms.ods.tms.yl_tmsnew_branch_shipment_stop import jms_ods__yl_tmsnew_branch_shipment_stop
from jms.ods.tms.yl_tmsnew_tms_shipment_stop import jms_ods__yl_tmsnew_tms_shipment_stop

out_days = 15  # 输出最近 15 天的分区
in_days = out_days + 6  # 读入最近 15 + 8 天的扫描数据

jms_dwd__dwd_s01_whole_operations_trace_dt = SparkSqlOperator(
    task_id='jms_dwd__dwd_s01_whole_operations_trace_dt',
    email=['rongguangfan@jtexpress.com','yl_bigdata@yl-scm.com'],
    name='jms_dwd__dwd_s01_whole_operations_trace_dt_{{ execution_date | date_add(1) | cst_ds }}',
    email_on_retry=True,
    sql='jms/dwd/dwd_s01_whole_operations_trace_dt/execute.hql',
    yarn_queue='pro',
    driver_memory='32G',
    driver_cores=12,
    executor_memory='24G',
    executor_cores=8,
    pool_slots=20,
    num_executors=30,
    conf={'spark.executor.memoryOverhead': '4G',
          'spark.dynamicAllocation.enabled': 'true',
          'spark.shuffle.service.enabled': 'true',
          'spark.dynamicAllocation.maxExecutors': 150,
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 20,
          'spark.sql.shuffle.partitions': out_days * 240,
          'spark.sql.autoBroadcastJoinThreshold': 209715200,
          'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
          },
    hiveconf={'hive.exec.dynamic.partition': 'true',
              'hive.exec.dynamic.partition.mode': 'nonstrict',
              'hive.exec.max.dynamic.partitions': out_days + 1,
              'hive.exec.max.dynamic.partitions.pernode': out_days + 1,
              },
    params={'out_days': out_days,
            'in_days': in_days,
            },
    execution_timeout=timedelta(hours=3),
)

jms_dwd__dwd_s01_whole_operations_trace_dt << [
    jms_ods__yl_tmsnew_branch_shipment_stop,
    jms_ods__yl_tmsnew_tms_shipment_stop,
    jms_dwd__dwd_yl_oms_oms_waybill_incre_dt,
    jms_dwd__dwd_s01_whole_operations_trace_mid_dt
]
